package com.example.myxm.view;

import android.content.Context;
import android.widget.TextView;
import com.example.myxm.R;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.List;

public class DateMarkerView extends MarkerView {
    private final TextView tvContent;
    private final List<String> fullDates;

    public DateMarkerView(Context context, int layoutResource, List<String> fullDates) {
        super(context, layoutResource);
        this.fullDates = fullDates;
        tvContent = findViewById(R.id.tvMarker);
    }

    // 每次重绘时会回调此方法
    @Override
    public void refreshContent(com.github.mikephil.charting.data.Entry e, Highlight highlight) {
        if (e instanceof BarEntry) {
            int idx = (int) ((BarEntry) e).getX();
            if (idx >= 0 && idx < fullDates.size()) {
                tvContent.setText(fullDates.get(idx));
            }
        }
        super.refreshContent(e, highlight);
    }

    // 控制 Marker 显示位置（偏上偏中）
    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2f), -getHeight() - 10);
    }
}
